package com.vincce.fitness.core.config;

import com.vincce.fitness.core.base.MyBatisRepository;
import com.vincce.fitness.core.base.RedisRunner;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.annotation.PreDestroy;
import javax.sql.DataSource;
import java.io.IOException;

/**
* Created by henry on 4/17/14.
*/
@Configuration
public class DataConfig {

	@Bean
	public DataSource dataSource(
			@Value("${db.driver}") String driver,
			@Value("${db.url}") String url,
			@Value("${db.username}") String username,
			@Value("${db.password}") String password
	) {
		DriverManagerDataSource dataSource = new DriverManagerDataSource();

		dataSource.setDriverClassName(driver);
		dataSource.setUrl(url);
		dataSource.setUsername(username);
		dataSource.setPassword(password);
		return dataSource;
	}


/*	@Bean
	public DataSourceTransactionManager transactionManager(DataSource dataSource){
		DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
		dataSourceTransactionManager.setDataSource(dataSource);
		return dataSourceTransactionManager;
	}

	@Bean
	public TransactionInterceptor transactionInterceptor(DataSourceTransactionManager dataSourceTransactionManager,Properties properties){
		TransactionInterceptor transactionInterceptor = new TransactionInterceptor();
		transactionInterceptor.setTransactionManager(dataSourceTransactionManager);
		transactionInterceptor.setTransactionAttributes(properties);

		return transactionInterceptor;
	}

	@Bean
	public Properties properties(
	) {
		Properties properties = new Properties();
		properties.put("*","PROPAGATION_REQUIRED");
		return properties;
	}

	@Bean
	public BeanNameAutoProxyCreator beanNameAutoProxyCreator(){
		BeanNameAutoProxyCreator beanNameAutoProxyCreator = new BeanNameAutoProxyCreator();
		String[] beanNames = new String[]{"*Mapper"};
		beanNameAutoProxyCreator.setBeanNames(beanNames);
		String[] interceptorNames = new String[]{"transactionInterceptor"};
		beanNameAutoProxyCreator.setInterceptorNames(interceptorNames);
		return beanNameAutoProxyCreator;
	}*/

	@Bean
	public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource, ApplicationContext applicationContext) throws IOException {
		SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
		sqlSessionFactory.setDataSource(dataSource);
		sqlSessionFactory.setTypeAliasesPackage("com.vincce.fitness.entity");
		sqlSessionFactory.setMapperLocations(applicationContext.getResources("classpath:/mappers/*/*-mapper.xml"));
		sqlSessionFactory.setConfigLocation(applicationContext.getResource("classpath:/mybatis-config.xml"));
	/*	Properties properties = new Properties();
		properties.put("mapUnderscoreToCamelCase", "true");
		sqlSessionFactory.setConfigurationProperties(properties);*/
		return sqlSessionFactory;
	}

	@Bean
	public MapperScannerConfigurer mapperScannerConfigurer() {
		MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
		mapperScannerConfigurer.setBasePackage("com.vincce.fitness.mappers");
		mapperScannerConfigurer.setAnnotationClass(MyBatisRepository.class);
		return mapperScannerConfigurer;
	}

	/*
	mybatis configuration end
	 */

	@Bean
	public RedisRunner redisRunner(
			@Value("${redis.servers}") String redisServers
	) {
		return new RedisRunner(redisServers);
	}

	@PreDestroy
	public void redisDestroy() {
		RedisRunner.stop();
	}

}
